package com.unproy.model.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;


@Entity
@Table(name = "TBCTGR")
public class Category implements Serializable, Comparable<Category> {
	private static final long	serialVersionUID	= -5706076161386920004L;
	private Short				categoryId;
	private String				description;
	private Set<SubCategory>	subCategories		= new HashSet<SubCategory>();
	private Set<Article>		articles			= new HashSet<Article>();
	
	@Id
	@SequenceGenerator(name = "categoryId", sequenceName = "SQCTGR")
	@GeneratedValue(generator = "categoryId", strategy = GenerationType.SEQUENCE)
	@Column(name = "CTGRID", unique = true, nullable = false)
	public Short getCategoryId() {
		return this.categoryId;
	}
	
	public void setCategoryId(Short categoryId) {
		this.categoryId = categoryId;
	}
	
	@Column(name = "DSCRPT", nullable = false)
	public String getDescription() {
		return this.description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "parentCategory")
	public Set<SubCategory> getSubCategories() {
		return this.subCategories;
	}
	
	public void setSubCategories(Set<SubCategory> subCategories) {
		this.subCategories = subCategories;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
	public Set<Article> getArticles() {
		return this.articles;
	}
	
	public void setArticles(Set<Article> articles) {
		this.articles = articles;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((this.categoryId == null) ? 0 : this.categoryId.hashCode());
		result = prime * result + ((this.description == null) ? 0 : this.description.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Category other = (Category) obj;
		if (this.categoryId == null) {
			if (other.categoryId != null)
				return false;
		} else if (!this.categoryId.equals(other.categoryId))
			return false;
		if (this.description == null) {
			if (other.description != null)
				return false;
		} else if (!this.description.equals(other.description))
			return false;
		return true;
	}
	
	@Override
	public int compareTo(Category category) {
		return getDescription().compareTo(category.getDescription());
	}
	
	@Override
	public String toString() {
		return getDescription();
	}
}
